package vn.sms.vsms.provider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;

import vn.sms.vsms.Contants;
import vn.sms.vsms.GlobalApplication;
import vn.sms.vsms.data.ServiceItem;
import android.app.Service;
import android.database.Cursor;
import android.provider.SyncStateContract.Constants;

import android.util.Log;
 

public class DbApdater {
	
	public static void executeSQL(String sql)
	{
		if(sql != null && !sql.isEmpty())
		{
			GlobalApplication.getDb().execSQL(sql);
		}
	}
	
	public static ArrayList<String> getAssetSQL() throws IOException{
		
		ArrayList<String> listStatement = new ArrayList<String>();
		
	    InputStream sql = GlobalApplication._().getAssets().open("sql.txt");
	    BufferedReader in=
	        new BufferedReader(new InputStreamReader(sql));
	    String str;

	    while ((str=in.readLine()) != null) {
	    	if(!str.isEmpty())
	    		listStatement.add(str);
	    }

	    in.close();
		return listStatement;
	}
	
	public static ArrayList<ServiceItem>  getTopMenu()
	{
		ArrayList<ServiceItem> menuList = new ArrayList<ServiceItem>();
		Cursor cursor ;
		cursor = GlobalApplication.getDb().query(DbSchema.GroupSchema.TABLE_NAME,null,DbSchema.GroupSchema.COLUMN_PARENT + "= ? AND " + DbSchema.GroupSchema.COLUMN_STATUS +" =?",new String[]{"0","1"},null,null,DbSchema.GroupSchema.COLUMN_ORDER);
		
		try {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {				
				Integer id  = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ID));
				String name =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_NAME));
				String icon =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ICON));
				
				ServiceItem item = new ServiceItem(id,name,0);
				int isguide = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_GUIDE));
				if(isguide > 0 )
				{
					Log.i("logging", "isguide:1");
					item.setIsguide(isguide);
				}
				item.setIcon(icon);
				menuList.add(item);
				
				
				cursor.moveToNext();
			}
		}catch(Exception e){e.printStackTrace();}
		finally {
		    cursor.close();
		}	
		return menuList;
		
	}
	
	public static ServiceItem getItemChildsByid(int id)
	{
		ServiceItem item = new ServiceItem();
		Cursor cursor ;
		cursor = GlobalApplication.getDb().query(DbSchema.GroupSchema.TABLE_NAME,null,DbSchema.GroupSchema.COLUMN_ID + "= ? AND " + DbSchema.GroupSchema.COLUMN_STATUS +" =?",new String[]{String.valueOf(id),"1"},null,null,DbSchema.GroupSchema.COLUMN_ORDER);
		try {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {								
				String name =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_NAME));
				Log.i("logging getItemChildsByid",name);
				int parent  =  cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_PARENT));
				item.setId(id);
				item.setName(name);	
				if(parent > 0)
				{
					item.setParent(parent);
				}					
				int day 	=  cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_DAY));
				if(day > 0 )
				{
					item.setDay(day);
				}
				String structure = cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_STRUCTURE));
				if(!structure.isEmpty())
				{
					item.setStructure(structure);
				}
				int phone 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_PHONE));
				if(phone >0 )
				{
					item.setPhone(phone);
				}
				int status 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_STATUS));
				if(status > 0 )
				{
					item.setStatus(status);
				}
				int order 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ORDER_ORIGIN));
				if(order > 0 )
				{
					item.setOrder(order);
				}
				int money 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_MONEY));
				if(money > 0 )
				{
					item.setMoney(money);
				}
				int isguide = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_GUIDE));
				if(isguide > 0 )
				{
					Log.i("logging", "isguide:1");
					item.setIsguide(isguide);
				}
				
				int ispopup = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_POPUP));
				if(ispopup > 0 )
				{
					item.setIspopup(ispopup);
				}
				
				String icon =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ICON));
				if(!icon.isEmpty())
				{
					item.setIcon(icon);
				}
				
				String popuptitle =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_POPUPTITLE));
				if(popuptitle!=null && !popuptitle.isEmpty())
				{
					item.setPopupTitle(popuptitle);
				}
					
				cursor.moveToNext();
			}
		}catch(Exception e){e.printStackTrace();}
		finally {
		    cursor.close();
		}
		
		if(item != null)
		{			
			getListRecursive(item);
			return item;
		}
		return item;
		
		 	
	}
	
	public static void getListRecursive(ServiceItem root)
	{
		
		ArrayList<ServiceItem> childs = new ArrayList<ServiceItem>();
		Cursor cursor ;
		cursor = GlobalApplication.getDb().query(DbSchema.GroupSchema.TABLE_NAME,null,DbSchema.GroupSchema.COLUMN_PARENT + "= ? AND " + DbSchema.GroupSchema.COLUMN_STATUS +" =?",new String[]{String.valueOf(root.getId()),"1"},null,null,DbSchema.GroupSchema.COLUMN_ORDER);
		
		try {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {	
				
				ServiceItem item = new ServiceItem();
				Integer id  = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ID));
				String name =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_NAME));
				int parent  =  cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_PARENT));
				item.setId(id);
				item.setName(name);	
				if(parent > 0)
				{
					item.setParent(parent);
				}					
				int day 	=  cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_DAY));
				if(day > 0 )
				{
					item.setDay(day);
				}
				String structure = cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_STRUCTURE));
				if(!structure.isEmpty())
				{
					item.setStructure(structure);
				}
				int phone 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_PHONE));
				if(phone >0 )
				{
					item.setPhone(phone);
				}
				int status 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_STATUS));
				if(status > 0 )
				{
					item.setStatus(status);
				}
				int order 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ORDER_ORIGIN));
				if(order > 0 )
				{
					item.setOrder(order);
				}
				int money 	= cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_MONEY));
				if(money > 0 )
				{
					item.setMoney(money);
				}
				int isguide = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_GUIDE));
				if(isguide > 0 )
				{
					Log.i("logging", "isguide:1");
					item.setIsguide(isguide);
				}
				
				int ispopup = cursor.getInt(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_POPUP));
				if(ispopup > 0 )
				{
					item.setIspopup(ispopup);
				}
				
				String icon =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_ICON));
				if(icon!= null && !icon.isEmpty())
				{
					item.setIcon(icon);
				}
				
				String popuptitle =  cursor.getString(cursor.getColumnIndex(DbSchema.GroupSchema.COLUMN_POPUPTITLE));
				if(popuptitle!=null && !popuptitle.isEmpty())
				{
					item.setPopupTitle(popuptitle);
				}
				
			    if(day > 0 && day == Contants.getDayofWeek() )
			    {
			    	childs.add(item);
			    }
			    else if(day == 0)
			    {
			    	childs.add(item);
			    }
				cursor.moveToNext();
				
			}
		}catch(Exception e){e.printStackTrace();}
		finally {
		    cursor.close();
		}
		
		if(childs.size() > 0)
		{
			root.setChilds(childs);			
			for(int index = 0 ; index < root.getChilds().size(); index ++)
			{					
				getListRecursive(root.getChilds().get(index));				
			}
		}
	}
	
}
